package letsjava.serial;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import letsjava.utils.NSConfig;

public class Foo extends AbstractFoo implements Serializable {
	private void readObject(ObjectInputStream s) throws IOException,
	        ClassNotFoundException {
		s.defaultReadObject();
		 
		int x = s.readInt();
		int y = s.readInt();
		System.out.println(x);
		initialize(x, y);
	}

	private void writeObject(ObjectOutputStream s) throws IOException {
		s.defaultWriteObject();
		
		s.writeInt(getX());
		s.writeInt(getY());
	}

	public Foo(int x, int y) {
		super(x, y);
	}

	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Foo a = new Foo(31,24);
		 
		ObjectOutputStream out = new ObjectOutputStream(
				new FileOutputStream(NSConfig.rootPath+"/tmp/result.obj") );
		// 试图将对象两次写入文件
		out.writeObject(a);
		out.close();
		
		
		ObjectInputStream oin = new ObjectInputStream(
				new FileInputStream(NSConfig.rootPath+"/tmp/result.obj"));
		//从文件依次读出两个文件
		Foo t1 = (Foo) oin.readObject();
		oin.close();
		System.out.println(t1);
	}
}
